<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Excel;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class ExcelController extends Controller
{
    //Excel文件导出功能 By Laravel学院
    public function export()
    {
        $cellData = [
            ['学号', '姓名', '成绩', '学号', '姓名', '成绩', '学号', '姓名', '成绩', '学号', '姓名', '成绩', '学号', '姓名', '成绩', '学号', '姓名', '成绩', '学号', '姓名', '成绩', '学号', '姓名', '成绩', '学号', '姓名', '成绩', '学号', '姓名', '成绩'],
            ['10001', 'AAAAA', '99', '10001', 'AAAAA', '99', '10001', 'AAAAA', '99', '10001', 'AAAAA', '99', '10001', 'AAAAA', '99', '10001', 'AAAAA', '99', '10001', 'AAAAA', '99', '10001', 'AAAAA', '99', '10001', 'AAAAA', '99', '10001', 'AAAAA', '99'],
            ['10001', 'AAAAA', '99', '10001', 'AAAAA', '99', '10001', 'AAAAA', '99', '10001', 'AAAAA', '99', '10001', 'AAAAA', '99', '10001', 'AAAAA', '99', '10001', 'AAAAA', '99', '10001', 'AAAAA', '99', '10001', 'AAAAA', '99', '10001', 'AAAAA', '99'],
            ['10001', 'AAAAA', '99', '10001', 'AAAAA', '99', '10001', 'AAAAA', '99', '10001', 'AAAAA', '99', '10001', 'AAAAA', '99', '10001', 'AAAAA', '99', '10001', 'AAAAA', '99', '10001', 'AAAAA', '99', '10001', 'AAAAA', '99', '10001', 'AAAAA', '99'],
        ];
        Excel::create(iconv('UTF-8', 'GBK', '学生成绩'), function ($excel) use ($cellData) {
            $excel->sheet('score', function ($sheet) use ($cellData) {
                $sheet->rows($cellData);
            });
        })->export('xls');
    }
    //Excel文件导入功能 By Laravel学院
    public function import()
    {
        return view('admin.excel.index');
    }

    public function importdata(Request $request)
    {
        $file = $request->file('name');
        $ext=$file->getClientOriginalExtension();
        $result = [];
        Excel::load($file->getRealPath(), function ($reader) use (&$result) {
            $reader = $reader->getSheet(0);
            $result = $reader->toArray();
            dd($result);
        });
    }
}
